package com.hisicom.scqxspxcjc.common.file.util;

import cn.hutool.core.util.StrUtil;
import com.hisicom.scqxspxcjc.common.file.exception.FileException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author zhonggls
 * @date 2020/4/29 10:52
 * @Explain
 */
public class MultipartFileToFile {
    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除本地临时文件
     *
     * @param file
     */
    public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }

    /**
     * base64转MultipartFile
     *
     * @param imgStr
     * @return
     */

    /**
     * 通过文件名判断并获文件的类型
     *
     * @param fileName 文件名
     * @return 文件的contentType
     */
    public static String getContentType(String fileName) {
        int ss = fileName.lastIndexOf(".");
        //文件的后缀名
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        if (".bmp".equalsIgnoreCase(fileExtension)) {
            return "image/bmp";
        }
        if (".gif".equalsIgnoreCase(fileExtension)) {
            return "image/gif";
        }
        if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension) || ".png".equalsIgnoreCase(fileExtension)) {
            return "image/jpeg";
        }
        if (".html".equalsIgnoreCase(fileExtension)) {
            return "text/html";
        }
        if (".txt".equalsIgnoreCase(fileExtension)) {
            return "text/plain";
        }
        if (".vsd".equalsIgnoreCase(fileExtension)) {
            return "application/vnd.visio";
        }
        if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
            return "application/vnd.ms-powerpoint";
        }
        if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
            return "application/msword";
        }
        if (".xml".equalsIgnoreCase(fileExtension)) {
            return "text/xml";
        }
//默认返回类型
        return "image/jpeg";
    }

    /**
     * 通过文件类型并获文件的后缀名
     *
     * @param file 文件名
     * @return 文件的contentType
     */
    public static String getFileNameByType(MultipartFile file) {
        String contentType = file.getContentType();
        if (StrUtil.isBlank(contentType)) {
            throw new FileException("201");
        }
        if ("image/bmp".equalsIgnoreCase(contentType)) {
            return ".bmp";
        }
        if ("image/gif".equalsIgnoreCase(contentType)) {
            return ".gif";
        }
        if ("image/jpeg".equalsIgnoreCase(contentType)) {
            return ".jpeg";
        }
        if ("image/png".equalsIgnoreCase(contentType)) {
            return ".png";
        }
        if ("text/html".equalsIgnoreCase(contentType)) {
            return ".html";
        }
        if ("text/plain".equalsIgnoreCase(contentType)) {
            return ".txt";
        }
        if ("application/vnd.visio".equalsIgnoreCase(contentType)) {
            return ".vsd";
        }
        if ("application/vnd.ms-powerpoint".equalsIgnoreCase(contentType)) {
            return ".ppt";
        }
        if ("application/msword".equalsIgnoreCase(contentType)) {
            return ".doc";
        }
        if ("text/xml".equalsIgnoreCase(contentType)) {
            return ".xml";
        }
        return contentType;
    }
}
